package com.ev.apis.controller.asl;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ev.apis.controller.mes.ProductionFeedingApiController;
import com.ev.apis.model.DsResultResponse;
import com.ev.asl.constant.AslConstant;
import com.ev.asl.domain.MoldingProductionInstructionDO;
import com.ev.asl.service.MoldingProductionInstructionService;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.*;
import com.ev.mes.domain.ProductionPlanDO;
import com.ev.mes.service.ProductionPlanService;
import com.ev.system.domain.UserDO;
import com.google.common.collect.Maps;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * 成型生产指示api
 *
 * @author 姚家乐
 * @since 2021/5/26 9:57
 */
@Api(value = "/", tags = "成型生产指示API")
@RestController
public class MoldingProductionInstructionApiController {

    @Autowired
    private MoldingProductionInstructionService service;

    @Autowired
    private ProductionPlanService productionPlanService;

    @Autowired
    private ProductionFeedingApiController productionFeedingApiController;

    /**
     * 保存成型生产指示信息
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/save", method = RequestMethod.POST, apiTitle = "保存成型生产指示信息")
    @ApiOperation("保存生产计划信息")
    //    @ApiImplicitParams({ @ApiImplicitParam(name = "indicateDate", value = "日期", dataType = "Date"),
    //                         @ApiImplicitParam(name = "mpiNo", value = "生产指示编号", dataType = "String"),
    //                         @ApiImplicitParam(name = "workTime", value = "工作时间", dataType = "Number"),
    //                         @ApiImplicitParam(name = "productionPlanId", value = "k3生产任务单id", dataType = "int"),
    //                         @ApiImplicitParam(name = "clientId", value = "k3生产任务单中客户ID", dataType = "int"),
    //                         @ApiImplicitParam(name = "clientName", value = "k3生产任务单中客户名", dataType = "String"),
    //                         @ApiImplicitParam(name = "materielId", value = "品名Id(物料Id)", dataType = "int"),
    //                         @ApiImplicitParam(name = "materielName", value = "品名(物料名)", dataType = "String"),
    //                         @ApiImplicitParam(name = "productCapacity", value = "产品容量", dataType = "Number"),
    //                         @ApiImplicitParam(name = "weight", value = "重量", dataType = "Number"),
    //                         @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "int"),
    //                         @ApiImplicitParam(name = "deviceNo", value = "设备编号", dataType = "String"),
    //                         @ApiImplicitParam(name = "specialNoteItem", value = "特记事项", dataType = "String"),
    //                         @ApiImplicitParam(name = "aGroupNumber", value = "A组数量", dataType = "Number"),
    //                         @ApiImplicitParam(name = "bGroupNumber", value = "B组数量", dataType = "Number"),
    //                         @ApiImplicitParam(name = "cGroupNumber", value = "C组数量", dataType = "Number") })
    public R save(MoldingProductionInstructionDO entity) {
        UserDO userDO = ShiroUtils.getUser();
        if (userDO != null) {
            entity.setStaffingId(userDO.getUserId());
            entity.setStaffingName(userDO.getName());
        }
        entity.setAGroupResidue(ObjectUtils.isEmpty(entity.getAGroupNumber()) ? BigDecimal.ZERO : entity.getAGroupNumber());
        entity.setBGroupResidue(ObjectUtils.isEmpty(entity.getBGroupNumber()) ? BigDecimal.ZERO : entity.getBGroupNumber());
        entity.setCGroupResidue(ObjectUtils.isEmpty(entity.getCGroupNumber()) ? BigDecimal.ZERO : entity.getCGroupNumber());
        entity.setAUpdateFirst(0);
        entity.setBUpdateFirst(0);
        entity.setCUpdateFirst(0);
        R rawMaterialRatio = productionFeedingApiController.getRawMaterialRatio(entity.getProductionPlanId(), null, null);
        if (ObjectUtil.isNotEmpty(rawMaterialRatio) && ObjectUtil.isNotEmpty(rawMaterialRatio.get("rawMaterialRatio"))) {
            entity.setRawMaterialRatio(JSON.toJSONString(rawMaterialRatio.get("rawMaterialRatio")));
//            entity.setRawMaterialRatio(rawMaterialRatio.get("rawMaterialRatio").toString());
        }
        ProductionPlanDO productionPlanDO = productionPlanService.get(entity.getProductionPlanId());
        entity.setMaterielId(productionPlanDO.getMaterielId());
        if (service.save(entity) > 0) {
//            ProductionPlanDO productionPlanDO = new ProductionPlanDO();
//            productionPlanDO.setId(entity.getProductionPlanId());
//            productionPlanDO.setHaveMpi(AslConstant.YES);
//            productionPlanService.update(productionPlanDO);
            Map<String, Object> result = Maps.newHashMapWithExpectedSize(1);
            result.put("id", entity.getId());
            return R.ok(result);
        }
        return R.error();
    }

    /**
     * 查看成型生产指示详情
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/detail", method = RequestMethod.POST, apiTitle = "查看成型生产指示详情")
    @ApiOperation("查看成型生产指示详情")
    public R detail(@ApiParam(value = "成型生产指示Id", required = true) @RequestParam(value = "id", defaultValue = "") Long id) {
        return R.ok(service.getDetailInfo(id));
    }

    /**
     * 查看成型生产指示详情Two
     *
     * @date 2019-11-27
     * @author gumingjie
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/detailTwo", method = RequestMethod.POST, apiTitle = "查看成型生产指示详情")
    @ApiOperation("查看成型生产指示详情")
    public R detailTwo(@ApiParam(value = "成型生产指示Id", required = true) @RequestParam(value = "id", defaultValue = "") Long id) {
        return R.ok(service.getDetailInfoTwo(id));
    }

    /**
     * 修改成型生产指示
     *
     * @param entity
     * @return
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/update", method = RequestMethod.POST, apiTitle = "修改成型生产指示")
    @ApiOperation("修改成型生产指示")
    public R update(MoldingProductionInstructionDO entity) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(entity.getId());
        if (AslConstant.MoldingProductionInstructionStatus.PLAN.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            R rawMaterialRatio = productionFeedingApiController.getRawMaterialRatio(entity.getProductionPlanId(), null, null);
            if (ObjectUtil.isNotEmpty(rawMaterialRatio) && ObjectUtil.isNotEmpty(rawMaterialRatio.get("rawMaterialRatio"))) {
                entity.setRawMaterialRatio(JSON.toJSONString(rawMaterialRatio.get("rawMaterialRatio")));
            }
            int update = service.update(entity);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }

        return R.error("非计划状态不能修改！");
    }

    /**
     * 删除
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/batchRemove", method = RequestMethod.POST, apiTitle = "删除成型生产指示")
    @ApiOperation("删除成型生产指示")
    public R batchRemove(@RequestParam("ids") Long[] ids) {
        Map<String, Object> pm = new HashMap<>();
        pm.put("ids", Arrays.asList(ids));
        List<MoldingProductionInstructionDO> list = service.list(pm);

        ids = list.stream().filter(e -> AslConstant.MoldingProductionInstructionStatus.PLAN
                .getType().equals(e.getStatus())).map(MoldingProductionInstructionDO::getId).toArray(Long[]::new);
        if (ids.length > AslConstant.ZERO) {
            int remove = service.batchRemove(ids);
            if (remove > 0) {
                return R.ok("删除成功");
            }
            return R.error("删除失败");
        }
        return R.error("删除失败");
    }

    /**
     * 删除
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/remove", method = RequestMethod.POST, apiTitle = "删除成型生产指示")
    @ApiOperation("删除成型生产指示")
    public R remove(@RequestParam("id") Long id) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.PLAN.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            int remove = service.remove(id);
            if (remove > 0) {
                return R.ok("删除成功");
            }
            return R.error("删除失败");
        }
        return R.error("删除失败");
    }

    /**
     * 审查
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/investigate", method = RequestMethod.GET, apiTitle = "审查成型生产指示")
    @ApiOperation("审查成型生产指示")
    public R investigate(@RequestParam("id") Long id, HttpServletRequest request) {

        UserDO userDO = ShiroUtils.getUser();

        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.PLAN.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO
                    .setStatus(AslConstant.MoldingProductionInstructionStatus.AUDITOR.getType());
            moldingProductionInstructionDO.setAuditorId(userDO.getUserId());
            moldingProductionInstructionDO.setAuditorName(userDO.getName());
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }
        return R.error();
    }

    /**
     * 反审查
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/reverseInvestigate", method = RequestMethod.GET, apiTitle = "反审查成型生产指示")
    @ApiOperation("反审查成型生产指示")
    public R reverseInvestigate(@RequestParam("id") Long id, HttpServletRequest request) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.AUDITOR.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO
                    .setStatus(AslConstant.MoldingProductionInstructionStatus.PLAN.getType());
            moldingProductionInstructionDO.setAuditorId(null);
            moldingProductionInstructionDO.setAuditorName(null);
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }
        return R.error("当前状态不能反审查！");
    }

    /**
     * 批准
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/ratify", method = RequestMethod.GET, apiTitle = "批准成型生产指示")
    @ApiOperation("批准成型生产指示")
    public R ratify(@RequestParam("id") Long id, HttpServletRequest request) {
        UserDO userDO = ShiroUtils.getUser();

        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.AUDITOR.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO
                    .setStatus(AslConstant.MoldingProductionInstructionStatus.APPROVED.getType());
            moldingProductionInstructionDO.setApproverId(userDO.getUserId());
            moldingProductionInstructionDO.setApproverName(userDO.getName());
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }
        return R.error();
    }

    /**
     * 反批准
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/reverseRatify", method = RequestMethod.GET, apiTitle = "反批准成型生产指示")
    @ApiOperation("反批准成型生产指示")
    public R reverseRatify(@RequestParam("id") Long id, HttpServletRequest request) {

        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.APPROVED.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO
                    .setStatus(AslConstant.MoldingProductionInstructionStatus.AUDITOR.getType());
            moldingProductionInstructionDO.setApproverId(null);
            moldingProductionInstructionDO.setApproverName(null);

            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }

        return R.error("当前状态不能反批准！");
    }

    /**
     * 开工
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/startWork", method = RequestMethod.GET, apiTitle = "开工成型生产指示")
    @ApiOperation("开工成型生产指示")
    public R startWork(@RequestParam("id") Long id) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.APPROVED.getType()
                .equals(moldingProductionInstructionDO.getStatus())
                || AslConstant.MoldingProductionInstructionStatus.SUSPENDED_STATE.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO
                    .setStatus(AslConstant.MoldingProductionInstructionStatus.HAS_COMMENCED.getType());
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }

        return R.error();
    }

    /**
     * 挂起
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/putUp", method = RequestMethod.GET, apiTitle = "挂起成型生产指示")
    @ApiOperation("挂起成型生产指示")
    public R putUp(@RequestParam("id") Long id) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.HAS_COMMENCED.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO.setStatus(
                    AslConstant.MoldingProductionInstructionStatus.SUSPENDED_STATE.getType());
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }
        return R.error();
    }

    /**
     * 结案
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/close", method = RequestMethod.GET, apiTitle = "结案成型生产指示")
    @ApiOperation("结案成型生产指示")
    public R close(@RequestParam("id") Long id) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.HAS_COMMENCED.getType()
                .equals(moldingProductionInstructionDO.getStatus())
                || AslConstant.MoldingProductionInstructionStatus.SUSPENDED_STATE.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO
                    .setStatus(AslConstant.MoldingProductionInstructionStatus.CASE_BEEN.getType());
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }
        return R.error();
    }

    /**
     * 反结案
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/reverseClose", method = RequestMethod.GET, apiTitle = "反结案成型生产指示")
    @ApiOperation("反结案成型生产指示")
    public R reverseClose(@RequestParam("id") Long id) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (AslConstant.MoldingProductionInstructionStatus.CASE_BEEN.getType()
                .equals(moldingProductionInstructionDO.getStatus())) {
            moldingProductionInstructionDO
                    .setStatus(AslConstant.MoldingProductionInstructionStatus.HAS_COMMENCED.getType());
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok();
            }
            return R.error();
        }
        return R.error("当前状态不能反结案！");
    }

    /**
     * 关闭成型生产指示
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/closeInstruction", method = RequestMethod.GET, apiTitle = "关闭成型生产指示")
    @ApiOperation("反结案成型生产指示")
    public R closeInstruction(@RequestParam("id") Long id) {
        MoldingProductionInstructionDO moldingProductionInstructionDO = service.get(id);
        if (moldingProductionInstructionDO != null ){
            moldingProductionInstructionDO.setCloseStatus(1);
            int update = service.update(moldingProductionInstructionDO);
            if (update > 0) {
                return R.ok("操作成功");
            }
            return R.error();
        }
        return R.error("未查询到数据！");
    }

    /**
     * 查看成型生产指示分页列表
     *
     * @param pageno
     * @param pagesize
     * @param delFlag
     * @param planNo
     * @param materielName
     * @param indicateDateStart
     * @param indicateDateEnd
     * @return
     */
    @EvApiByToken(value = "/apis/MoldingProductionInstruction/list", method = RequestMethod.POST, apiTitle = "查看成型生产指示分页列表")
    @ApiOperation("查看成型生产指示分页列表")
    public R list(@ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno", defaultValue = "1") int pageno,
                  @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize", defaultValue = "20") int pagesize,
                  @ApiParam(value = "刪除状态") @RequestParam(value = "delFlag", defaultValue = "", required = false) Integer delFlag,
                  @ApiParam(value = "生产指示编号") @RequestParam(value = "planNo", defaultValue = "", required = false) String planNo,
                  @ApiParam(value = "状态") @RequestParam(value = "status", defaultValue = "", required = false) Integer status,
                  @ApiParam(value = "状态") @RequestParam(value = "closeStatus", defaultValue = "", required = false) Integer closeStatus,
                  @ApiParam(value = "状态集合") @RequestParam(value = "statusList", defaultValue = "", required = false) List<Integer> statusList,
                  @ApiParam(value = "品名") @RequestParam(value = "materielName", defaultValue = "", required = false) String materielName,
                  @ApiParam(value = "品名") @RequestParam(value = "serialNo", defaultValue = "", required = false) String serialNo,
                  @ApiParam(value = "日期的起始时间") @RequestParam(value = "indicateDateStart", defaultValue = "", required = false) Date indicateDateStart,
                  @ApiParam(value = "日期的结束时间") @RequestParam(value = "indicateDateEnd", defaultValue = "", required = false) Date indicateDateEnd,
                  @ApiParam(value = "是否首检(0:未首检，1：已首检)") @RequestParam(value = "haveFirstCheck", defaultValue = "", required = false) Integer haveFirstCheck) {

        Map<String, Object> params = Maps.newHashMapWithExpectedSize(10);
        params.put("offset", (pageno - 1) * pagesize);
        params.put("limit", pagesize);
        params.put("delFlag", delFlag);
        params.put("status", status);
        params.put("statusList", statusList);
        params.put("planNo", planNo);
        params.put("materielName", materielName);
        params.put("serialNo", serialNo);
        params.put("closeStatus", closeStatus);
        params.put("indicateDateStart", indicateDateStart);
        params.put("indicateDateEnd", indicateDateEnd);
        //首检和成型日报不强关联
//        params.put("haveFirstCheck", haveFirstCheck);

        Map<String, Object> results = Maps.newHashMapWithExpectedSize(1);
        List<Map<String, Object>> data = service.listForMap(params);
        for (Map<String, Object> datum : data) {
            if (!ObjectUtils.isEmpty(datum.get("specification"))) {
                datum.put("materielName", datum.get("materielName") + "-" + datum.get("specification"));
            }
        }
        if (data.size() > 0) {
            int total = service.countForMap(params);

            params.remove("offset");
            params.remove("limit");
            List<Map<String, Object>> data2 = service.listForMap(params);
            BigDecimal aGroupNumberTotal = BigDecimal.ZERO;
            BigDecimal bGroupNumberTotal = BigDecimal.ZERO;
            BigDecimal cGroupNumberTotal = BigDecimal.ZERO;
            BigDecimal aGroupResidueTotal = BigDecimal.ZERO;
            BigDecimal bGroupResidueTotal = BigDecimal.ZERO;
            BigDecimal cGroupResidueTotal = BigDecimal.ZERO;
            for (Map<String, Object> map : data2) {
                aGroupNumberTotal = aGroupNumberTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("aGroupNumber") ? map.get("aGroupNumber") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                bGroupNumberTotal = bGroupNumberTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("bGroupNumber") ? map.get("bGroupNumber") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                cGroupNumberTotal = cGroupNumberTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("cGroupNumber") ? map.get("cGroupNumber") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                aGroupResidueTotal = aGroupResidueTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("aGroupResidue") ? map.get("aGroupResidue") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                bGroupResidueTotal = bGroupResidueTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("bGroupResidue") ? map.get("bGroupResidue") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                cGroupResidueTotal = cGroupResidueTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("cGroupResidue") ? map.get("cGroupResidue") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
            }

            for (Map<String, Object> map : data) {
//                map.put("indicateDate", DateUtils.format((Date) map.get("indicateDate")));
                map.put("startDate", ObjectUtil.isEmpty(map.get("startDate")) ? null : DateUtils.format((Date) map.get("startDate"), "yyyy-MM-dd HH:mm:ss"));
                map.put("endDate", ObjectUtil.isEmpty(map.get("endDate")) ? null : DateUtils.format((Date) map.get("endDate"), "yyyy-MM-dd HH:mm:ss"));
            }

            Map<String, Object> result = Maps.newHashMapWithExpectedSize(4);
            result.put("data", new DsResultResponse(pageno, pagesize, total, data));
            result.put("aGroupNumberTotal", aGroupNumberTotal);
            result.put("bGroupNumberTotal", bGroupNumberTotal);
            result.put("cGroupNumberTotal", cGroupNumberTotal);
            result.put("aGroupResidueTotal", aGroupResidueTotal);
            result.put("bGroupResidueTotal", bGroupResidueTotal);
            result.put("cGroupResidueTotal", cGroupResidueTotal);
            results.put("data", result);
        }
        return R.ok(results);
    }

    @EvApiByToken(value = "/apis/MoldingProductionInstruction/list/exportExcel", method = RequestMethod.GET, apiTitle = "成型生产指示分页列表(导出)")
    @ApiOperation("成型生产指示分页列表(导出)")
    public void exportExcel(@ApiParam(value = "刪除状态") @RequestParam(value = "delFlag", defaultValue = "", required = false) Integer delFlag,
                            @ApiParam(value = "生产指示编号") @RequestParam(value = "planNo", defaultValue = "", required = false) String planNo,
                            @ApiParam(value = "品名") @RequestParam(value = "materielName", defaultValue = "", required = false) String materielName,
                            @ApiParam(value = "日期的起始时间") @RequestParam(value = "indicateDateStart", defaultValue = "", required = false) Date indicateDateStart,
                            @ApiParam(value = "日期的结束时间") @RequestParam(value = "indicateDateEnd", defaultValue = "", required = false) Date indicateDateEnd,
                            HttpServletRequest request, HttpServletResponse response,
                            ModelMap modelMap) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(5);
        params.put("delFlag", delFlag);
        params.put("planNo", planNo);
        params.put("materielName", materielName);
        params.put("indicateDateStart", indicateDateStart);
        params.put("indicateDateEnd", indicateDateEnd);
        List<Map<String, Object>> data = service.listForMap(params);
        if (!CollectionUtils.isEmpty(data)) {

            BigDecimal aGroupNumberTotal = BigDecimal.ZERO;
            BigDecimal bGroupNumberTotal = BigDecimal.ZERO;
            BigDecimal cGroupNumberTotal = BigDecimal.ZERO;
            for (Map<String, Object> map : data) {
                map.put("indicateDateString", DateUtils.format((Date) map.get("indicateDate")));
                if (AslConstant.MoldingProductionInstructionStatus.PLAN.getType()
                        .equals(map.get("status"))) {
                    map.put("statusString",
                            AslConstant.MoldingProductionInstructionStatus.PLAN.getName());
                }
                if (AslConstant.MoldingProductionInstructionStatus.AUDITOR.getType()
                        .equals(map.get("status"))) {
                    map.put("statusString",
                            AslConstant.MoldingProductionInstructionStatus.AUDITOR.getName());
                }
                if (AslConstant.MoldingProductionInstructionStatus.APPROVED.getType()
                        .equals(map.get("status"))) {
                    map.put("statusString",
                            AslConstant.MoldingProductionInstructionStatus.APPROVED.getName());
                }
                if (AslConstant.MoldingProductionInstructionStatus.HAS_COMMENCED.getType()
                        .equals(map.get("status"))) {
                    map.put("statusString",
                            AslConstant.MoldingProductionInstructionStatus.HAS_COMMENCED.getName());
                }
                if (AslConstant.MoldingProductionInstructionStatus.SUSPENDED_STATE.getType()
                        .equals(map.get("status"))) {
                    map.put("statusString",
                            AslConstant.MoldingProductionInstructionStatus.SUSPENDED_STATE.getName());
                }
                if (AslConstant.MoldingProductionInstructionStatus.CASE_BEEN.getType()
                        .equals(map.get("status"))) {
                    map.put("statusString",
                            AslConstant.MoldingProductionInstructionStatus.CASE_BEEN.getName());
                }
                aGroupNumberTotal = aGroupNumberTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("aGroupNumber") ? map.get("aGroupNumber") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                bGroupNumberTotal = bGroupNumberTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("aGroupNumber") ? map.get("aGroupNumber") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                cGroupNumberTotal = cGroupNumberTotal
                        .add(new BigDecimal(String.valueOf(map.containsKey("aGroupNumber") ? map.get("aGroupNumber") : "0")))
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
            }

            Map<String, Object> map = Maps.newHashMap();
            map.put("list", JSONUtils.format(data));
            map.put("aGroupNumberTotal", aGroupNumberTotal);
            map.put("bGroupNumberTotal", bGroupNumberTotal);
            map.put("cGroupNumberTotal", cGroupNumberTotal);

            ClassPathResource classPathResource = new ClassPathResource(
                    "poi/molding_production_instruction.xlsx");
            TemplateExportParams result = new TemplateExportParams(classPathResource.getPath());
            modelMap.put(TemplateExcelConstants.FILE_NAME, "成型生产指示");
            modelMap.put(TemplateExcelConstants.PARAMS, result);
            modelMap.put(TemplateExcelConstants.MAP_DATA, map);
            PoiBaseView.render(modelMap, request, response,
                    TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
        }
    }
}
